package com.ymatou.iapi.userservice.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.login.parameter.BindWeiXinUseroRequestDtoBean;
import com.ymatou.iapi.login.service.BindWeiXinUseroRequestDtoCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.BatchUserBriefInfoBean;
import com.ymatou.iapi.userservice.parameter.enums.AppSourceEnum;
import com.ymatou.iapi.userservice.service.BatchUserBriefInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;

/**
 * 批量获取用户简要信息 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchUserBriefInfo {
	private static BatchUserBriefInfoBean batchuserbriefinfoBean;
	private static BatchUserBriefInfoCall batchuserbriefinfoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量获取用户简要信息");
	}

	@Before
	public void caseUp() {
		batchuserbriefinfoBean = new BatchUserBriefInfoBean();
		batchuserbriefinfoCall = new BatchUserBriefInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchuserbriefinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchUserBriefInfo_001() {
		Logger.start(true, "批量获取用户简要信息成功");
		try {
			List<Integer> UserIdList = new ArrayList<>();

			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			int uid = urrc.getUserid();
			UserIdList.add(uid);

			batchuserbriefinfoBean.setUserIdList(UserIdList);
			batchuserbriefinfoCall.setData(batchuserbriefinfoBean);
			batchuserbriefinfoCall.callService();
			Logger.verifyEquals("200", batchuserbriefinfoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", batchuserbriefinfoCall.getString("Success"), "验证Success");
			JsonArray Result = batchuserbriefinfoCall.getJsonObject().get("Result").getAsJsonArray();
			Logger.verifyEquals(1, Result.size(), "验证Result.size");
			for (JsonElement jsonElement : Result) {
				Logger.verifyEquals(regbean.getLoginId(), jsonElement.getAsJsonObject().get("LoginId").getAsString(), "验证LoginId");
				Logger.verifyEquals(regbean.getLoginMobile(), jsonElement.getAsJsonObject().get("LoginMobile").getAsString(), "验证LoginMobile");
				// 默认头像地址
				Logger.verifyEquals("1", jsonElement.getAsJsonObject().get("UserLevel").getAsString(), "验证UserLevel");
				Logger.verifyEquals(regbean.getRegisterType(), jsonElement.getAsJsonObject().get("UserType").getAsInt(), "验证UserType");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_BatchUserBriefInfo_002() {
		Logger.start(true, "不输入userid失败");
		try {
			List<Integer> UserIdList = new ArrayList<>();
			batchuserbriefinfoBean.setUserIdList(UserIdList);
			batchuserbriefinfoCall.setData(batchuserbriefinfoBean);
			batchuserbriefinfoCall.callService();
			Logger.verifyEquals("100", batchuserbriefinfoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("用户Id列表不能为空且必须小于100", batchuserbriefinfoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchUserBriefInfo_003() {
		Logger.start(true, "批量获取多个用户简要信息成功");
		try {
			List<Integer> UserIdList = new ArrayList<>();
			Map<Integer, UserRegisterInfoRequestDtoBean> m = new HashMap<Integer, UserRegisterInfoRequestDtoBean>();
			for (int i = 0; i < 5; i++) {
				UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
				UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
				JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
				String token = registerResult.get("Token").getAsString();
				int uid = urrc.getUserid();
				UserIdList.add(uid);
				m.put(uid, regbean);
			}
			batchuserbriefinfoBean.setUserIdList(UserIdList);
			batchuserbriefinfoCall.setData(batchuserbriefinfoBean);
			batchuserbriefinfoCall.callService();
			Logger.verifyEquals("200", batchuserbriefinfoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", batchuserbriefinfoCall.getString("Success"), "验证Success");
			JsonArray Result = batchuserbriefinfoCall.getJsonObject().get("Result").getAsJsonArray();
			Logger.verifyEquals(UserIdList.size(), Result.size(), "验证Result.size");
			for (JsonElement jsonElement : Result) {
				UserRegisterInfoRequestDtoBean regbean = m.get(jsonElement.getAsJsonObject().get("UserId").getAsInt());
				Logger.verifyEquals(regbean.getLoginId(), jsonElement.getAsJsonObject().get("LoginId").getAsString(), "验证LoginId");
				Logger.verifyEquals(regbean.getLoginMobile(), jsonElement.getAsJsonObject().get("LoginMobile").getAsString(), "验证LoginMobile");
				// 默认头像地址
				Logger.verifyEquals("1", jsonElement.getAsJsonObject().get("UserLevel").getAsString(), "验证UserLevel");
				Logger.verifyEquals(regbean.getRegisterType(), jsonElement.getAsJsonObject().get("UserType").getAsInt(), "验证UserType");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_BatchUserBriefInfo_004() {
		Logger.start(true, "参数超过20失败");
		try {
			List<Integer> UserIdList = new ArrayList<>();
			batchuserbriefinfoBean.setUserIdList(UserIdList);
			for(int i=1;i<=101;i++){
				UserIdList.add(i);
			}
			batchuserbriefinfoCall.setData(batchuserbriefinfoBean);
			batchuserbriefinfoCall.callService();
			Logger.verifyEquals("100", batchuserbriefinfoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyIsContains("用户Id列表不能为空且必须小于100", batchuserbriefinfoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchUserBriefInfo_005() {
		Logger.start(true, "批量获取用户简要信息成功-返回微信绑定信息");
		try {
			List<Integer> UserIdList = new ArrayList<>();

			//创建新用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			int uid=urrc.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			UserIdList.add(uid);
			//修改头像
			JUserCallService.SetUserLogoCall(uid, "http://file.iapi.ymatou.com/G01/M00/1F/55/rBBlD11nhWKAOjQzAABaeGeA9IE1416792");
			//获取用户信息然后绑定ymt
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			Integer userId=registerResult.get("UserId").getAsInt();
			BindWeiXinUseroRequestDtoBean bean=JUserCallService.initBindWeiXinUseroRequestDtoBean(token);
			bean.getWeiXinUser().setRegSource(AppSourceEnum.WxApp);
			BindWeiXinUseroRequestDtoCall wx = JUserCallService.bindWeiXinUseroRequestDto(bean);
			
			
			batchuserbriefinfoBean.setUserIdList(UserIdList);
			batchuserbriefinfoCall.setData(batchuserbriefinfoBean);
			batchuserbriefinfoCall.callService();
			Logger.verifyEquals("200", batchuserbriefinfoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", batchuserbriefinfoCall.getString("Success"), "验证Success");
			JsonArray Result = batchuserbriefinfoCall.getJsonObject().get("Result").getAsJsonArray();
			Logger.verifyEquals(1, Result.size(), "验证Result.size");
			for (JsonElement jsonElement : Result) {
				Logger.verifyEquals(regbean.getLoginId(), jsonElement.getAsJsonObject().get("LoginId").getAsString(), "验证LoginId");
				Logger.verifyEquals(regbean.getLoginMobile(), jsonElement.getAsJsonObject().get("LoginMobile").getAsString(), "验证LoginMobile");
				// 默认头像地址
				Logger.verifyEquals("1", jsonElement.getAsJsonObject().get("UserLevel").getAsString(), "验证UserLevel");
				Logger.verifyEquals(regbean.getRegisterType(), jsonElement.getAsJsonObject().get("UserType").getAsInt(), "验证UserType");
				//WxNickName":null,"WxLogoUrl"
				batchuserbriefinfoCall.callService();
				Result = batchuserbriefinfoCall.getJsonObject().get("Result").getAsJsonArray();
				Logger.verifyEquals(bean.getWeiXinUser().getHeadimgurl()
						, Result.get(0).getAsJsonObject().get("WxLogoUrl").getAsString(), "验证WxLogoUrl");
				Logger.verifyEquals(bean.getWeiXinUser().getName(), Result.get(0).getAsJsonObject().get("WxNickName").getAsString(), "验证WxNickName");

				}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchUserBriefInfo_006() {
		Logger.start(true, "批量获取用户简要信息成功-返回微信绑定信息");
		try {
			List<Integer> UserIdList = new ArrayList<>();

			//创建新用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			int uid=urrc.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			UserIdList.add(uid);
			//修改头像
			JUserCallService.SetUserLogoCall(uid, "http://file.iapi.ymatou.com/G01/M00/1F/55/rBBlD11nhWKAOjQzAABaeGeA9IE1416792");
			//获取用户信息然后绑定ymt
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			Integer userId=registerResult.get("UserId").getAsInt();
			BindWeiXinUseroRequestDtoBean bean=JUserCallService.initBindWeiXinUseroRequestDtoBean(token);
			bean.getWeiXinUser().setRegSource(AppSourceEnum.WxApp);
			BindWeiXinUseroRequestDtoCall wx = JUserCallService.bindWeiXinUseroRequestDto(bean);
			
			
			batchuserbriefinfoBean.setUserIdList(UserIdList);
			batchuserbriefinfoCall.setData(batchuserbriefinfoBean);
			batchuserbriefinfoCall.callService();
			Logger.verifyEquals("200", batchuserbriefinfoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", batchuserbriefinfoCall.getString("Success"), "验证Success");
			JsonArray Result = batchuserbriefinfoCall.getJsonObject().get("Result").getAsJsonArray();
			Logger.verifyEquals(1, Result.size(), "验证Result.size");
			for (JsonElement jsonElement : Result) {
				Logger.verifyEquals(regbean.getLoginId(), jsonElement.getAsJsonObject().get("LoginId").getAsString(), "验证LoginId");
				Logger.verifyEquals(regbean.getLoginMobile(), jsonElement.getAsJsonObject().get("LoginMobile").getAsString(), "验证LoginMobile");
				// 默认头像地址
				Logger.verifyEquals("1", jsonElement.getAsJsonObject().get("UserLevel").getAsString(), "验证UserLevel");
				Logger.verifyEquals(regbean.getRegisterType(), jsonElement.getAsJsonObject().get("UserType").getAsInt(), "验证UserType");
				//WxNickName":null,"WxLogoUrl"
				batchuserbriefinfoCall.callService();
				Result = batchuserbriefinfoCall.getJsonObject().get("Result").getAsJsonArray();
				Logger.verifyEquals(bean.getWeiXinUser().getHeadimgurl()
						, Result.get(0).getAsJsonObject().get("WxLogoUrl").getAsString(), "验证WxLogoUrl");
				Logger.verifyEquals(bean.getWeiXinUser().getName(), Result.get(0).getAsJsonObject().get("WxNickName").getAsString(), "验证WxNickName");

				}
				JUserCallService.updateWxHeadImg(userId, "update");
				batchuserbriefinfoCall.callService();
				Thread.sleep(11000);
				batchuserbriefinfoCall.callService();
				Logger.verifyEquals(bean.getWeiXinUser().getName(), Result.get(0).getAsJsonObject().get("WxNickName").getAsString(), "验证WxNickName");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchUserBriefInfo_007() {
		Logger.start(true, "批量获取用户简要信息成功-增加字段NotNeedWeiXinInfo不返回wx信息");
		try {
			List<Integer> UserIdList = new ArrayList<>();

			//创建新用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			int uid=urrc.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			UserIdList.add(uid);
			//修改头像
			JUserCallService.SetUserLogoCall(uid, "http://file.iapi.ymatou.com/G01/M00/1F/55/rBBlD11nhWKAOjQzAABaeGeA9IE1416792");
			//获取用户信息然后绑定ymt
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			Integer userId=registerResult.get("UserId").getAsInt();
			BindWeiXinUseroRequestDtoBean bean=JUserCallService.initBindWeiXinUseroRequestDtoBean(token);
			bean.getWeiXinUser().setRegSource(AppSourceEnum.WxApp);
			BindWeiXinUseroRequestDtoCall wx = JUserCallService.bindWeiXinUseroRequestDto(bean);
			
			batchuserbriefinfoBean.setNotNeedWeiXinInfo(true);
			batchuserbriefinfoBean.setUserIdList(UserIdList);
			batchuserbriefinfoCall.setData(batchuserbriefinfoBean);
			batchuserbriefinfoCall.callService();
			Logger.verifyEquals("200", batchuserbriefinfoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", batchuserbriefinfoCall.getString("Success"), "验证Success");
			JsonArray Result = batchuserbriefinfoCall.getJsonObject().get("Result").getAsJsonArray();
			Logger.verifyEquals(1, Result.size(), "验证Result.size");
			for (JsonElement jsonElement : Result) {
				Logger.verifyEquals(regbean.getLoginId(), jsonElement.getAsJsonObject().get("LoginId").getAsString(), "验证LoginId");
				Logger.verifyEquals(regbean.getLoginMobile(), jsonElement.getAsJsonObject().get("LoginMobile").getAsString(), "验证LoginMobile");
				// 默认头像地址
				Logger.verifyEquals("1", jsonElement.getAsJsonObject().get("UserLevel").getAsString(), "验证UserLevel");
				Logger.verifyEquals(regbean.getRegisterType(), jsonElement.getAsJsonObject().get("UserType").getAsInt(), "验证UserType");
				//WxNickName":null,"WxLogoUrl"
				batchuserbriefinfoCall.callService();
				Result = batchuserbriefinfoCall.getJsonObject().get("Result").getAsJsonArray();
				Logger.verifyEquals(true
						, Result.get(0).getAsJsonObject().get("WxLogoUrl").isJsonNull(), "验证WxLogoUrl不返回");
				Logger.verifyEquals(true, Result.get(0).getAsJsonObject().get("WxNickName").isJsonNull(), "验证WxNickName");

				}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}